import 'package:ac/model/ac_login_model.dart';
import 'package:ac/page/ac_message/ac_message_logic.dart';
import 'package:ac/route/ac_route_pages.dart';
import 'package:get/get.dart';
import 'package:rongcloud_im_wrapper_plugin/src/rongcloud_im_classes.dart';

import '../api/ac_urls.dart';
import 'ac_pv_manager.dart';

class AcChatUtil {
  static Future<void> openChat(String userId) async {
    LogPvManger.stat(page: LogPages.chatPage, subPage: userId);
    var result = await AcUrls.getUserInfo.get(
      queryParameters: {"userId": userId},
      showLoading: true,
      showErrorMessage: false,
    );
    if (result?["code"] == 0) {
      var data = result['data'];
      var userinfo = Userinfo.fromJson(data);
      Get.toNamed(AcRouteNames.chatPage, arguments: {
        "conversation": ACConversation(null, userinfo),
      });
    }
  }

  static Future<ACConversation?> getChatWithConversation(
      RCIMIWConversation conversation) async {
    var result = await AcUrls.getUserInfo.get(
      queryParameters: {"userId": conversation.targetId},
      showLoading: false,
      showErrorMessage: false,
    );
    if (result?["code"] == 0) {
      var data = result['data'];
      var userinfo = Userinfo.fromJson(data);
      return ACConversation(conversation, userinfo);
    }
    return null;
  }
}
